  
<cfcomponent displayname="libDAO" hint="I abstract data access for lib.">

	<cffunction name="init" access="public" returntype="libDAO" output="false" displayname="lib Constructor" hint="I initialize a lib.">
		<cfargument name="datasourceSettings" type="any" required="true" />
		
		<cfset variables.dsn = arguments.datasourceSettings.getConfigSetting("dsn") />
		<cfset variables.dbtype = arguments.datasourceSettings.getConfigSetting("dbtype") />

		<cfreturn this />
	</cffunction>

 
	<cffunction name="save" returntype="any" output="false" access="public" 	hint="I save a lib">
		<cfargument name="lib" type="lib" hint="I am the lib to be saved" required="true"/>

		<cfif arguments.lib.hasId()>
			<cfif arguments.lib.getlibid() gt 0>
				<cfreturn update(arguments.lib)/>
			<cfelse>
				<cfreturn create(arguments.lib)/>
			</cfif>
		<cfelse>
			<cfreturn create(arguments.lib)/>
		</cfif>

	</cffunction>

	<cffunction name="create" access="remote" returntype="any" output="false">
		<cfargument name="libBean" type="lib" required="true" hint="I am the lib object into which data is created." />/>

		<cfquery name="insRec" datasource="#variables.dsn#">
			insert into lib(
			name, description, parent, created, updated, isPublic, ownerID
			)
			values(
				<cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.libBean.getname()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.libBean.getdescription()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.libBean.getparent()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.libBean.getcreated()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.libBean.getupdated()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.libBean.getisPublic()#"  /> 
				, <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.libBean.getownerID()#"  /> 
				
				)
		</cfquery>

		<cfquery name="qGetID" datasource="#variables.dsn#">
			SELECT
			<cfswitch expression="#variables.dbtype#">
				<cfcase value="mysql">
					LAST_INSERT_ID()
				</cfcase>
				<cfcase value="mssql">
					@@IDENTITY
				</cfcase>
				<cfcase value="msaccess">
					MAX(libid )
					
				</cfcase>
				<cfdefaultcase>
					<cfthrow message="Unknown Datasource Vendor!">
				</cfdefaultcase>
			</cfswitch>
				AS newid
			FROM lib
		</cfquery>
		
		
			<cfset libBean.setlibid(qGetID.newid)>
		
		
		<cfreturn libBean/>
	</cffunction>
	
	<cffunction name="read" access="public" returntype="any" output="false" hint="CRUD method read.">
		<cfargument name="libid" type="numeric" required="true" hint="I am the ID of the database record to read." />
		<cfset var libBean = createobject("component","lib").init() />
		
		<cfquery name="libSelect" datasource="#variables.dsn#">
			SELECT * 
			FROM	lib	
			WHERE
				libid = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.libid#"  /> 
				
		</cfquery>

		<cfif libSelect.recordCount >
			<cfscript>
				libBean.setlibid(libSelect.libid);				
				libBean.setname(libSelect.name);				
				libBean.setdescription(libSelect.description);				
				libBean.setparent(libSelect.parent);				
				libBean.setcreated(libSelect.created);				
				libBean.setupdated(libSelect.updated);				
				libBean.setisPublic(libSelect.isPublic);				
				libBean.setownerID(libSelect.ownerID);				
				
			</cfscript>			
			
		</cfif>
		
		<cfreturn libBean />
	</cffunction>

	<cffunction name="update" returntype="any" output="false" hint="CRUD method update">
		<cfargument name="libBean" type="lib" required="true" />	
		<cfset var libUpdate = 0 >

		<cfquery name="libUpdate" datasource="#variables.dsn#" >
			UPDATE lib
			SET
				name = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.libBean.getname()#"  /> 
				, description = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.libBean.getdescription()#"  /> 
				, parent = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.libBean.getparent()#"  /> 
				, created = <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.libBean.getcreated()#"  /> 
				, updated = <cfqueryparam cfsqltype="cf_sql_TIMESTAMP" value="#arguments.libBean.getupdated()#"  /> 
				, isPublic = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.libBean.getisPublic()#"  /> 
				, ownerID = <cfqueryparam cfsqltype="cf_sql_VARCHAR" value="#arguments.libBean.getownerID()#"  /> 
				
			WHERE
				libid = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.libBean.getlibid()#"  /> 
				
		</cfquery>
		
		<cfreturn libBean />
	</cffunction>


	<cffunction name="delete" returntype="void" output="false" hint="CRUD method delete" >
		<cfargument name="libBean" type="lib" required="true" />		

		<cfset var libDelete = 0 >

		<cfquery name="libDelete" datasource="#variables.dsn#" >
			DELETE FROM lib
			WHERE
				libid = <cfqueryparam cfsqltype="cf_sql_INTEGER" value="#arguments.libBean.getlibid()#"  /> 
				
		</cfquery>
	</cffunction>

</cfcomponent>

  
